package cn.school.controller.app;

import cn.school.attendance.dormitory.pojo.AttendanceDto;
import cn.school.attendance.dormitory.service.DormitoryAttendanceService;
import cn.school.attendance.dormitory.vo.StudentAttendanceVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * App 学生宿舍考勤
 *
 * @author fangjin.tao
 * @updateTime 2022/12/27 13:41
 * @throws
 */
@RestController
@RequestMapping("/app/attendance")
@Api(tags = "App AttendanceController=> APP考勤信息")
@Slf4j
public class AppAttendanceController {

//    @Resource
//    FenceService searchAttendance;

//    @ApiOperation(value = "查询当前时间是否在打卡时间范围内")
//    @PostMapping(value = "/searchAttendance")
//    public CommonResult<List<FenceConfigVO>> searchAttendance() {
//        return CommonResult.success(searchAttendance.findCurrTimeUseFence());
//    }
//
//    @ApiOperation(value = "查询当前位置是否在指定范围内")
//    @PostMapping(value = "/fenceRange")
//    public CommonResult<Boolean> fenceRange(@RequestBody FenceLocationVO vo) {
//        return CommonResult.success(searchAttendance.fenceRange(vo.getLongitude(),vo.getLatitude(),vo.getFenceId()));
//    }


//	 ================================ new attendance dao =========================================

    @Autowired
    DormitoryAttendanceService attendanceService;

    @ApiOperation("获取某一日考勤数据")
    @PostMapping(value = "/getAttendanceStatsByDay")
    public CommonResult getAttendanceStatsByDay(@RequestBody AttendanceDto query){
        Map<String, Object> map = attendanceService.getAttendanceStatsByDay(query);
        return CommonResult.success(map);
    }

    @ApiOperation("获取某楼栋某一日考勤数据")
    @PostMapping(value = "/getAttendanceStatsByBuildAndDay")
    public CommonResult getAttendanceStatsByBuildAndDay(@RequestBody AttendanceDto query){
        List<Object> list = attendanceService.getAttendanceStatsByBuildAndDay(query);
        return CommonResult.success(list);
    }

    @ApiOperation("获取某宿舍某一日考勤数据")
    @PostMapping("/getAttendanceStatsByLocationAndDay")
    public CommonResult getAttendanceStatsByLocationAndDay(@RequestBody AttendanceDto query){
        List<StudentAttendanceVO> mapList = attendanceService.getAttendanceStatsByLocationAndDay(query);
        return CommonResult.success(mapList);
    }

    @ApiOperation("修改学生考勤状态")
    @PostMapping("/updateAttendanceStats")
    public CommonResult updateAttendanceStats(@RequestBody StudentAttendanceVO dto){
        return attendanceService.updateAttendance(dto) > 0 ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation("批量修改学生考勤状态")
    @PostMapping("/updateAttendanceStatusBatch")
    public CommonResult updateAttendanceStatusBatch(@RequestBody List<StudentAttendanceVO> list){
        boolean b = attendanceService.updateAttendanceStatusBatch(list);
        if(b){
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("获取学生宿舍考勤（0-查询未归 1-查询在寝 2-查询请假）")
    @PostMapping("/getStudentAttendanceList")
    public CommonResult getStudentAttendanceList(@RequestBody Map<String,String> query){
        IPage<StudentAttendanceVO> page = attendanceService.getStudentAttendanceList(query);
        return CommonResult.success(CommonPage.restPage(page));
    }
}
